package com.xw.controller.other;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.xw.dto.CustomMenu;
import com.xw.service.WeChatMsgHandlerService;
import com.xw.utils.vx.AesException;
import com.xw.utils.vx.WXBizMsgCrypt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 9/14/21 3:58 PM
 * @Version: 1.0
 */
@RestController
@RequestMapping("/wx")
public class WxController {

    @Value("${wechat.mpAppId}")
    private String appId;

    @Value("${wechat.mpAppSecret}")
    private String secret;

    @Value("${wechat.token}")
    private String token;

    @Value("${wechat.encodingAesKey}")
    private String encodingAesKey;


    private final WeChatMsgHandlerService weChatMsgHandlerService;

    public WxController(WeChatMsgHandlerService weChatMsgHandlerService) {
        this.weChatMsgHandlerService = weChatMsgHandlerService;
    }

    @RequestMapping("/handleMessage")
    public String handleMessage(@RequestParam(value = "signature") String signature,
                                @RequestParam(value = "timestamp") String timestamp,
                                @RequestParam(value = "nonce") String nonce,
                                @RequestBody String str) throws AesException {

        WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token,
                encodingAesKey, appId);

        //解密微信服务器发来的用户加密数据
        String postData = wxcpt.decryptMsg(signature, timestamp, nonce, str);
        System.out.println("postData");
        System.out.println(postData);

        return weChatMsgHandlerService.handleMessage(postData);

    }


//    //在线预定
//    @GetMapping("/onlineReserve")
//    public String onlineReserve() {
//
//
//        return null;
//
//    }


//    @RequestMapping("/handleMessage")
//    public String checkSignature(@RequestParam(value = "signature") String signature, @RequestParam(value = "timestamp") String timestamp, @RequestParam(value = "nonce") String nonce, @RequestParam(value = "echostr") String echostr) throws AesException {
//        WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token,
//                encodingAesKey, appId);
//        return wxcpt.verifyUrl(signature, timestamp, nonce, echostr);
//    }

    @GetMapping("/createMenu")
    public void createMenu() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret;
        String response = restTemplate.getForObject(url, String.class);
        JSON parse = JSONUtil.parse(response);
        String accessToken = parse.getByPath("access_token").toString();

        String createUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
        CustomMenu customMenu = new CustomMenu();
        String s = restTemplate.postForObject(createUrl, JSONUtil.parse(customMenu.getMenu()), String.class);
        System.out.println(s);
    }


}